package com.gao.transportapi.controller;

import com.gao.transportapi.common.convention.result.PageDTO;
import com.gao.transportapi.common.convention.result.Result;
import com.gao.transportapi.pojo.dto.AccountUpdateDTO;
import com.gao.transportapi.pojo.query.AccountQuery;
import com.gao.transportapi.pojo.vo.AccountBalanceVO;
import com.gao.transportapi.pojo.vo.AccountVO;
import com.gao.transportapi.service.IAccountsService;
import jakarta.annotation.Resource;
import org.springframework.core.annotation.Order;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 账户API控制器
 */
@RestController
@RequestMapping("/accounts")
@Order(1) // 较高的优先级，确保在ProxyController之前处理
public class AccountController {

    @Resource
    IAccountsService accountService;



    /**
     * 批量创建账户
     *
     * @param keyList 账户信息列表
     * @return 创建结果
     */
    @PostMapping("/batch")
    public Result<Void> batchCreateAccounts(@RequestBody List<String> keyList) {
        accountService.batchCreateAccounts(keyList);
        return Result.success();
    }


    /**
     * 根据id删除账户
     *
     * @param id 账户ID
     * @return 删除结果
     */
    @DeleteMapping("/{id}")
    public Result<Void> deleteAccountById(@PathVariable Integer id) {
        accountService.deleteAccountById(id);
        return Result.success();
    }

    /**
     * 根据id更新账户
     *
     * @param id 账户ID
     * @param accountDTO 账户信息
     * @return 更新结果
     */
    @PutMapping("/{id}")
    public Result<AccountVO> updateAccountById(@PathVariable Integer id, @RequestBody AccountUpdateDTO accountDTO) {
        accountDTO.setId(id);
        return Result.success(accountService.updateAccountById(accountDTO));
    }

    /**
     * 批量更新账户余额
     * 实现层直接发请求获取当前余额并更新
     *
     * @param accountIds
     * @return 更新后的账户余额信息
     */
    @PutMapping("/balances")
    public Result<List<AccountBalanceVO>> batchUpdateAccountsBalance(@RequestBody List<Integer> accountIds) {
        return Result.success(accountService.batchUpdateAccountsBalance(accountIds));
    }

    /**
     * 分页查询账户列表
     *
     * @param query 查询条件
     * @return 查询结果
     */
    @GetMapping
    public Result<PageDTO<AccountVO>> listAccounts(AccountQuery query) {
        return Result.success(accountService.listAccounts(query));
    }

    /**
     * 根据id获取账户信息
     *
     * @param id 账户ID
     * @return 账户信息
     */
    @GetMapping("/{id}")
    public Result<AccountVO> getAccountById(@PathVariable Integer id) {
        return Result.success(accountService.getAccountById(id));
    }


} 